/**
 * 
 */
package com.sim.contact.tool;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.sim.contact.tool.contacts.ContactAPISdk5;

/**
 * TODO
 * @author lcw 2014-6-16 下午4:28:04
 */
public class MainActivity extends Activity implements OnClickListener{

	private Button	convert_btn;
	private ContactAPISdk5	contactAPISdk5;
	private Button	delete_btn;
	private ProgressDialog	progressDialog;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		
		initalViews();
		
		contactAPISdk5 = new ContactAPISdk5();
		contactAPISdk5.setCr(getContentResolver());
		
	}
	
	private void initalViews(){
		convert_btn = (Button)findViewById(R.id.convert_btn);
		convert_btn.setOnClickListener(this);
		delete_btn = (Button)findViewById(R.id.delete_btn);
		delete_btn.setOnClickListener(this);
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.convert_btn:
			AlertDialog.Builder builder = new Builder(this);
			builder.setMessage(R.string.sureCopy);
			builder.setTitle(R.string.notic);
			builder.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					copyContactToSIM();
				}
			});
			builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();
			break;
		case R.id.delete_btn:
			builder = new Builder(this);
			builder.setMessage(R.string.sureDeleteAllSIMContacs);
			builder.setTitle(R.string.notic);
			builder.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					deleteContactFromSIM();
				}
			});
			builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();
			break;

		default:
			break;
		}
	}
	
	private void copyContactToSIM(){
		progressDialog = ProgressDialog.show(MainActivity.this,getResources().getString(R.string.notic),getResources().getString(R.string.doingCopy));
		progressDialog.show();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				final int rslt = contactAPISdk5.converContactToSIMCard();
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						if(progressDialog!=null && progressDialog.isShowing()){
							progressDialog.dismiss();
						}
						Toast.makeText(MainActivity.this, String.format(getResources().getString(R.string.hasCopyedCount), rslt), Toast.LENGTH_LONG).show();
					}
				});
			}
		}).start();
	}
	private void deleteContactFromSIM(){
		progressDialog = ProgressDialog.show(MainActivity.this,getResources().getString(R.string.notic),getResources().getString(R.string.doingDeleting));
		progressDialog.show();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				final int rslt = contactAPISdk5.deleteSIMContacts();
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						if(progressDialog!=null && progressDialog.isShowing()){
							progressDialog.dismiss();
						}
						Toast.makeText(MainActivity.this, String.format(getResources().getString(R.string.hasDeletedCount), rslt), Toast.LENGTH_LONG).show();
					}
				});
			}
		}).start();
	}
}
